1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
import { createSlug } from '@/core/utils/slug';
import toTitleCase from '@/core/utils/toTitleCase';
import variantSearchApi from '@/lib/product/api/variantSearchApi';
import axios from 'axios';
import _ from 'lodash-contrib';
import { create } from 'xmlbuilder';
export async function getServerSideProps({ res, query }) {
const PPN = process.env.NEXT_PUBLIC_PPN;
const titleContent = 'Indoteknik.com: B2B Industrial Supply & Solution';
const descriptionContent =
'Temukan pilihan produk B2B Industri & Alat Teknik untuk Perusahaan, UMKM & Pemerintah dengan lengkap, mudah dan transparan.';
const { page } = query;
const limit = 5000;
const queries = {
limit,
page: page.replace('.xml', ''),
priceFrom: 1,
orderBy: 'popular',
fq: ['image_s:["" TO *] AND publish_b:true AND price_tier1_v2_f:[1 TO *]'],
// product_rating_f: '[8 TO *]',
// price_tier1_v2_f: '[1 TO *]',
};
const products = await variantSearchApi({ query: _.toQuery(queries) });
const brandsData = {};
const categoriesData = {};
let i = 0;
const productItems = [];
for (const product of products.response.products) {
const productUrl = createSlug(
'/shop/product/variant/',
product.name,
product.id,
true,
);
const productId = product.code != '' ? product.code : product.id;
const regexHtmlTags = /(<([^>]+)>)/gi;
product.description = product.description
?.replace(regexHtmlTags, ' ')
.trim();
const defaultProductDescription =
'Indoteknik.com menawarkan berbagai produk industri, konstruksi, dan teknik terpercaya. Temukan mesin industri, peralatan listrik, alat pengukur, dan banyak lagi. Pengalaman berbelanja mudah dengan deskripsi produk lengkap, spesifikasi teknis, dan gambar jelas. Pembayaran aman, pengiriman cepat ke seluruh Indonesia. Solusi lengkap untuk kebutuhan Kantor, Industri & Teknik Anda.';
if (!product.description) {
product.description = defaultProductDescription;
}
let categoryName = null;
let brandId = product.manufacture?.id ?? null;
let categoryId = null;
let category_level_1 = null;
if (brandId && brandId in brandsData) {
categoryId = brandsData[brandId]?.category_ids?.[0] ?? null;
} else {
const solrBrand = await getBrandById(brandId);
brandsData[brandId] = solrBrand;
categoryId = solrBrand?.category_ids?.[0] ?? null;
}
if (product.categories[0]?.id) {
const getProductTemplate = await getProductTemplateId(
product?.productTemplate ?? null,
);
i = i + 1;
const id =
getProductTemplate?.category_parent_ids?.length > 0
? getProductTemplate.category_parent_ids[0]
: null;
if (id) {
category_level_1 = await getCategoryById(id);
} else {
category_level_1 = null;
}
} else {
category_level_1 = null;
}
if (categoryId && categoryId in categoriesData) {
categoryName = categoriesData[categoryId]?.name_s ?? null;
} else {
const solrCategory = await getCategoryById(categoryId);
categoriesData[categoryId] = solrCategory;
categoryName = solrCategory?.name_s ?? null;
}
const availability = 'in_stock';
const item = {
'g:id': { '#text': productId },
'g:title': { '#text': toTitleCase(product.name) },
'g:description': { '#text': product.description },
'g:link': { '#text': productUrl },
'g:image_link': { '#text': product.image + '?variant=True' },
'g:condition': { '#text': 'new' },
'g:availability': { '#text': availability },
'g:brand': { '#text': product.manufacture?.name || '' },
'g:price': {
'#text': `${Math.round(product.lowestPrice.price * PPN)} IDR`,
},
// 'g:custom_label_3': { '#text': product.categories[0]?.name || 'Tidak Ada Kategori' },
'g:custom_label_4': {
'#text': category_level_1?.name_s || 'Tidak Ada Kategori',
},
};
if (product.stockTotal == 0) {
item['g:custom_label_0'] = { '#text': 'Stok Tidak Tersedia' };
} else {
item['g:custom_label_1'] = { '#text': 'Stok Tersedia' };
}
if (product.categories?.[0]?.name) {
item['g:custom_label_2'] = {
'#text': product.categories[0].name,
};
}
if (product.lowestPrice.discountPercentage > 0) {
item['g:sale_price'] = {
'#text': `${Math.round(product.lowestPrice.priceDiscount * PPN)} IDR`,
};
}
productItems.push(item);
}
const googleMerchant = {
rss: {
'@xmlns:g': 'http://base.google.com/ns/1.0',
'@version': '2.0',
channel: {
title: { '#text': `<![CDATA[${titleContent}]]>` },
link: { '#text': process.env.SELF_HOST },
description: { '#text': `<![CDATA[${descriptionContent}]]>` },
item: productItems,
},
},
};
res.setHeader('Content-Type', 'text/xml;charset=iso-8859-1');
res.write(create(googleMerchant).end());
res.end();
return { props: {} };
}
const getBrandById = async (id) => {
const brand = await axios(
`${process.env.SOLR_HOST}/solr/brands/select?q=id:${id}`,
);
return brand.data.response.docs[0] ?? null;
};
const getProductTemplateId = async (id) => {
const category = await axios(
`${process.env.SOLR_HOST}/solr/product/select?q=id:${id}`,
);
return category.data.response.docs[0] ?? null;
};
const getCategoryById = async (id) => {
const category = await axios(
`${process.env.SOLR_HOST}/solr/categories/select?q=id:${id}`,
);
return category.data.response.docs[0] ?? null;
};
export default function GoogleMerchantPage() {
return null;
}
|